package cn.yunhe.control;

import cn.yunhe.entity.FriendMessage;
import cn.yunhe.entity.User;
import cn.yunhe.server.FriendServer;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/FriendMessageServlet")
public class FriendMessageServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession(); //获取session对象
        //获取用户名和密码
        String username = (String) session.getAttribute("username");
        String password = (String) session.getAttribute("password");
        User user = new User(username,password); //新建用户对象
        FriendServer friendServer = new FriendServer();
        List<FriendMessage> list = friendServer.GetFriendMessage(user);
        if (list!=null&&list.size()>0){
            request.setAttribute("FriendMessageList",list);
            request.getRequestDispatcher("friendMessage.jsp").forward(request,response);
        }else {
            request.setAttribute("result","暂无好友请求消息!!");
            request.getRequestDispatcher("friendMessage.jsp").forward(request,response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
